//
//  searchResultViewCell.m
//  有材网
//
//  Created by qijia on 16/1/21.
//  Copyright © 2016年 肖扬. All rights reserved.
//

#import "searchResultViewCell.h"
#import "EGOImageView.h"
#import "homeModel.h"
#import "Unity.h"

@implementation searchResultViewCell
@synthesize smallImg,markImg,titleLab,addressLab,priceLab,sizeLab,futureLab,stateImg,lab,lineView;
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self ){
        self.backgroundColor = [UIColor whiteColor];
        self.lineView = [[UIView alloc]initWithFrame:CGRectMake(10, 103.5, SWIDTH-20, 0.5)];
        [self.lineView setBackgroundColor:[UIColor grayColor]];
        lineView.alpha = 0.2;
        [self.contentView addSubview:self.lineView];
        
        self.lab = [self createLabelFrame:CGRectMake(10, 5, SWIDTH - 20, 10) title:@"特价推荐" titleColor:BLACKColor tag:0];
        lab.font = [UIFont systemFontOfSize:10];
        lab.hidden = YES;
        [self.contentView addSubview:lab];
        
        EGOImageView *tempSmallImg = [[EGOImageView alloc]initWithPlaceholderImage:[UIImage imageNamed:@"默认加载图片"]];
        tempSmallImg.frame = CGRectMake(10, 10, 104, 82);
        tempSmallImg.backgroundColor = [UIColor clearColor];
        [tempSmallImg setContentMode:UIViewContentModeScaleToFill];
        self.smallImg = tempSmallImg;
        [self.contentView addSubview:self.smallImg];
        
        
        self.stateImg = [self createImageViewFrame:CGRectMake(0, 0, 32, 28) image:nil imageName:@"特" tag:0];
        [self.smallImg addSubview:self.stateImg];
        
        UILabel *tempTitleLab = [self createLabelFrame:CGRectMake(124, 8, SWIDTH - 114, 20) title:@"" titleColor:UIColorFromRGB(0x333333) tag:0];
        tempTitleLab.font = [UIFont systemFontOfSize:14];
        tempTitleLab.contentMode = UIViewContentModeTop;
        self.titleLab = tempTitleLab;
        //        self.titleLab.numberOfLines = 2;
        [self.contentView addSubview:self.titleLab];
        
        UILabel *tempSizeLab = [self createLabelFrame:CGRectMake(124, 33, 200, 12) title:@"" titleColor:UIColorFromRGB(0x666666) tag:0];
        tempSizeLab.font = [UIFont systemFontOfSize:12];
        self.sizeLab = tempSizeLab;
        [self.contentView addSubview:self.sizeLab];
        
        UILabel *tempAddressLab = [self createLabelFrame:CGRectMake(124, 54, 150, 12) title:@"" titleColor:UIColorFromRGB(0x999999) tag:0];
        tempAddressLab.font = [UIFont systemFontOfSize:12];
        self.addressLab = tempAddressLab;
        [self.contentView addSubview:self.addressLab];
        
        UILabel *tempPriceLab = [self createLabelFrame:CGRectMake(124, 74, 100, 12) title:@"" titleColor:UIColorFromRGB(0xf94101) tag:0];
        tempPriceLab.font = [UIFont systemFontOfSize:12];
        self.priceLab = tempPriceLab;
        [self.contentView addSubview:self.priceLab];
        
        UILabel *tempFutureLab = [self createLabelFrame:CGRectMake(self.priceLab.frame.size.width + self.priceLab.frame.origin.x + 5, 72, 36, 18) title:@"现货" titleColor:[UIColor whiteColor] tag:0];
        tempFutureLab.font = [UIFont systemFontOfSize:12];
        self.futureLab = tempFutureLab;
        self.futureLab.backgroundColor = UIColorFromRGB(0x2578c3);
        self.futureLab.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:self.futureLab];

        
    }
    return self;
}


-(void)LoadViewFromObject:(homeModelSupply *)obj{
    homeModelSupply *model= (homeModelSupply *)obj;
    [self.smallImg setImage:[UIImage imageNamed:@"默认加载图片"]];
    int y = 0;
    if (self.lab.hidden == YES) {
        y = 0;
    }else{
        y = 20;
    }
    self.lineView.frame = CGRectMake(10, 103.5+y, SWIDTH-20, 0.5);
    self.titleLab.frame = CGRectMake(124, 8+y, SWIDTH - 114, 20);
    self.smallImg.frame = CGRectMake(10, 10+y, 104, 82);
    self.sizeLab.frame = CGRectMake(124, 33+y, 200, 12);
    self.addressLab.frame = CGRectMake(124, 54+y, 150, 12);
    self.priceLab.frame = CGRectMake(124, 74+y, 100, 12);
    self.futureLab.frame = CGRectMake(self.priceLab.frame.size.width + self.priceLab.frame.origin.x + 5, 72+y, 36, 18);
    if (![Unity isBlankString:model.pics]) {
        if (![Unity isBlankString:[model.pics objectForKey:@"thumb100x100"]]) {
            NSMutableArray *arr = [model.pics objectForKey:@"thumb100x100"];
            if ([arr count]!=0) {
                [self.smallImg setImageURL:[NSURL URLWithString:[arr objectAtIndex:0]]];
                self.smallImg.frame = CGRectMake(10, 10, 104, 82);
            }
        }
    }
    if (![Unity isBlankString:model.title]) {
        self.titleLab.text = model.title;
    }else{
        NSString *title = @"";
        if (![Unity isBlankString:model.brand_name]) {
            title = [title stringByAppendingString:[NSString stringWithFormat:@"%@",model.brand_name]];
        }
        if (![Unity isBlankString:model.material_name]) {
            title = [title stringByAppendingString:[NSString stringWithFormat:@" %@",model.material_name]];
        }
        self.titleLab.text = title;
    }
    NSString *address = @"";
    if ([Unity isBlankString:model.province_name]&&[Unity isBlankString:model.city_name]) {
        [self.addressLab setHidden:YES];
    }else{
        if (![Unity isBlankString:model.province_name]) {
            address = [address stringByAppendingString:[NSString stringWithFormat:@"%@",model.province_name]];
        }
        if (![Unity isBlankString:model.city_name]) {
            address = [address stringByAppendingString:[NSString stringWithFormat:@"%@",model.city_name]];
        }
        self.addressLab.text = address;
    }
    if ([Unity isBlankString:model.length]&&[Unity isBlankString:model.width]&&[Unity isBlankString:model.thickness]) {
        [self.sizeLab setHidden:YES];
        self.addressLab.frame = CGRectMake(124, 54+y, 150, 12);
        self.priceLab.frame = CGRectMake(124, 74+y, 100, 12);
    }else{
        [self.sizeLab setHidden:NO];
        self.addressLab.frame = CGRectMake(124, 54+y, 150, 12);
        self.priceLab.frame = CGRectMake(124, 74+y, 100, 12);
        NSString *size = @"";
//        if (![Unity isBlankString:model.length]) {
//            size = [size stringByAppendingString:[NSString stringWithFormat:@"%@mm",model.length]];
//        }
//        if (![Unity isBlankString:model.width]) {
//            size = [size stringByAppendingString:[NSString stringWithFormat:@"*%@mm",model.width]];
//        }
//        if (![Unity isBlankString:model.thickness]) {
//            size = [size stringByAppendingString:[NSString stringWithFormat:@"*%@mm",model.thickness]];
//        }
        int material_id = [model.material_id intValue];
        if (material_id >= 100000000 && material_id <200000000) { //板材
            if (![Unity isBlankString:model.length]) {
                size = [size stringByAppendingString:[NSString stringWithFormat:@"%@mm",model.length]];
                if ([model.length isEqualToString:@"0"]||[model.length isEqualToString:@"0.0"]) {
                    size = @"";
                }
            }
            if (![Unity isBlankString:model.width]) {
                size = [size stringByAppendingString:[NSString stringWithFormat:@"*%@mm",model.width]];
                if ([model.width isEqualToString:@"0"]||[model.width isEqualToString:@"0.0"]) {
                    size = @"";
                }
            }
            if (![Unity isBlankString:model.thickness]) {
                size = [size stringByAppendingString:[NSString stringWithFormat:@"*%@mm",model.thickness]];
                if ([model.thickness isEqualToString:@"0"]||[model.thickness isEqualToString:@"0.0"]) {
                    size = @"";
                }
            }
        }else if (material_id >=200000000 && material_id <= 300000000){ //装饰纸
            if (![Unity isBlankString:model.length]) {
                size = [size stringByAppendingString:[NSString stringWithFormat:@"%@尺",model.length]];
                if ([model.length isEqualToString:@"0"]||[model.length isEqualToString:@"0.0"]) {
                    size = @"";
                }
            }
            if (![Unity isBlankString:model.width]) {
                size = [size stringByAppendingString:[NSString stringWithFormat:@"*%@尺",model.width]];
                if ([model.width isEqualToString:@"0"]||[model.width isEqualToString:@"0.0"]) {
                    size = @"";
                }
            }
            if (![Unity isBlankString:model.thickness]) {
                size = [size stringByAppendingString:[NSString stringWithFormat:@"*%@尺",model.thickness]];
                if ([model.thickness isEqualToString:@"0"]||[model.thickness isEqualToString:@"0.0"]) {
                    size = @"";
                }
            }
        }else if (material_id >=300000000 && material_id < 400000000){ //门板
            if (![Unity isBlankString:model.length]) {
                size = [size stringByAppendingString:[NSString stringWithFormat:@"%@mm",model.length]];
                if ([model.length isEqualToString:@"0"]||[model.length isEqualToString:@"0.0"]) {
                    size = @"";
                }
            }
            if (![Unity isBlankString:model.width]) {
                size = [size stringByAppendingString:[NSString stringWithFormat:@"*%@mm",model.width]];
                if ([model.width isEqualToString:@"0"]||[model.width isEqualToString:@"0.0"]) {
                    size = @"";
                }
            }
            if (![Unity isBlankString:model.thickness]) {
                size = [size stringByAppendingString:[NSString stringWithFormat:@"*%@mm",model.thickness]];
                if ([model.thickness isEqualToString:@"0"]||[model.thickness isEqualToString:@"0.0"]) {
                    size = @"";
                }
            }
        }
        self.sizeLab.text= size;
    }
    if (![Unity isBlankString:model.price]) {
        self.priceLab.text = [NSString stringWithFormat:@"%@元",model.price];
        if (![Unity isBlankString:model.unit]) {
            self.priceLab.text = [NSString stringWithFormat:@"%@元/%@",model.price,model.unit];
        }
    }
    UIFont *font = [UIFont systemFontOfSize:12];
    CGSize titleSize = [self.priceLab.text sizeWithFont:font constrainedToSize:CGSizeMake(MAXFLOAT, 12)];
    self.futureLab.frame = CGRectMake(titleSize.width + 129, 72+y, 36, 18);
    if ([model.future isEqualToString:@"0"]) {
        self.futureLab.backgroundColor = BLUEColor;
        self.futureLab.text = @"现货";
    }else{
        self.futureLab.backgroundColor = GREENColor;
        self.futureLab.text = @"期货";
    }
    if ([model.is_sales isEqualToString:@"1"]) {
        [self.stateImg setHidden:NO];
    }else{
        [self.stateImg setHidden:YES];
    }

}

- (CGSize)sizeThatFits:(CGSize)size
{
    return CGSizeMake(size.width, 104);
}
//创建UILabel  tag值为三位数
- (UILabel *)createLabelFrame:(CGRect)frame
                        title:(NSString *)title
                   titleColor:(UIColor *)color
                          tag:(NSInteger)tag{
    UILabel *tempLabel = [[UILabel alloc]initWithFrame:frame];
    [tempLabel setBackgroundColor:[UIColor clearColor]];
    [tempLabel setText:title];
    [tempLabel setTag:tag];
    [tempLabel setTextColor:color];
    return tempLabel;
}
//创建UIButton tag值为四位数
- (UIButton *)createButtonFrame:(CGRect)frame
                          title:(NSString *)title
                 viewController:(id)viewController
                       selector:(SEL)selector
                            tag:(NSInteger)tag{
    UIButton *tempButton = [UIButton buttonWithType:0];
    [tempButton setFrame:frame];
    [tempButton setTag:tag];
    [tempButton setBackgroundColor:[UIColor clearColor]];
    [tempButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [tempButton setTitle:title forState:UIControlStateNormal];
    [tempButton addTarget:viewController action:selector forControlEvents:UIControlEventTouchUpInside];
    return tempButton;
}
//创建UIImageView tag值为五位数
- (UIImageView *)createImageViewFrame:(CGRect)frame
                                image:(UIImage *)image
                            imageName:(NSString *)imageName
                                  tag:(NSInteger)tag{
    UIImageView *tempImageView = [[UIImageView alloc]initWithFrame:frame];
    [tempImageView setBackgroundColor:[UIColor clearColor]];
    if (image) {
        tempImageView.image = image;
    }else{
        tempImageView.image = [UIImage imageNamed:imageName];
    }
    [tempImageView setTag:tag];
    return tempImageView;
}


@end
